import { observable, action, toJS } from 'mobx';
import {WeaTableNew } from 'comsMobx';
import {WeaLocaleProvider} from 'ecCom';
import * as TaskReport from '../../apis/task/taskReport'
const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;
export default class TaskReportStore {
    @observable tableStore = new TableStore(); // new table
    @observable selectedKey = 2;
    //高级查询
    @observable hrmids = [];//人员
    @observable workerType = "1";
    @observable subcompanyids = [];//分部
    @observable cpyincludesub = "3";//包含分部
    @observable departmentids = [];//部门
    @observable deptincludesub ="3";//包含部门
    @observable beginDate = "";//创建开始时间
    @observable endDate = "";//创建结束时间
    personType = "1";//统计对象
    @observable personType1 = "1";
    @observable personType2 = "";
    @observable personType3 = "";
    @observable datalist = [];
    @observable selectedRowKeys = [];
    selectedRows = [];
    @observable loading =false;
    @observable exportloading =false;
    @observable options = [];
    @observable isechart = 0;

    //获取人员的weatable
    @action
    getWorkerReport = (params) => {
        this.getCommonParams(params);
        if(this.selectedKey==2){
            this.loading =true;
        }
        this.isechart = 0;
        TaskReport.getWorkerReport(params).then(action(res => {
            if(res.status){
                this.endDate = res.endDateStr;
                this.beginDate = res.beginDateStr;
                if(this.selectedKey==1){
                    this.tableStore.getDatas(res.datas);
                }else{
                    this.datalist = res.datas;
                }
            }
            if(this.selectedKey==2){
                this.loading =false;
            }
        }))
    }
    @action
    viewDetail = (params) =>{
        params.beginDate = this.beginDate;
        params.endDate = this.endDate;
        let ptype = "";
        if(this.personType1==1){
            ptype +=",1";
        }
        if(this.personType2==1){
            ptype +=",2";
        }
        if(this.personType3==1){
            ptype +=",3";
        }
        params.personType = ptype;
        this.selectedKey = 1;
        TaskReport.getWorkerReport(params).then(action(res => {
            if(res.status){
                this.tableStore.getDatas(res.datas);
            }
        }))
    }
    @action
    getCommonParams(params){
        params.type = this.selectedKey;
        params.workerType = this.workerType;
        params.cpyincludesub = this.cpyincludesub;
        params.deptincludesub = this.deptincludesub;
        params.beginDate = this.beginDate;
        params.endDate = this.endDate;
        let ptype = "";
        if(this.personType1==1){
            ptype +=",1";
        }
        if(this.personType2==1){
            ptype +=",2";
        }
        if(this.personType3==1){
            ptype +=",3";
        }
        params.personType = ptype;
        if(this.hrmids && this.hrmids.length>0){
            params.hrmids = this.hrmids[0].id;
        }
        if(this.departmentids && this.departmentids.length>0){
            params.departmentids = this.departmentids[0].id;
        }
        if(this.subcompanyids && this.subcompanyids.length>0){
            params.subcompanyids = this.subcompanyids[0].id;
        }
    }
    @action
    clearParams = () =>{
        this.hrmids = [];//人员
        this.workerType = "1";
        this.subcompanyids = [];//分部
        this.cpyincludesub = "3";//包含分部
        this.departmentids = [];//部门
        this.deptincludesub ="3";//包含部门
        this.beginDate = "";//创建开始时间
        this.endDate = "";//创建结束时间
        this.personType = "1";//统计对象
        this.personType1 = "1";//统计对象
        this.personType2 = "";//统计对象
        this.personType3 = "";//统计对象
    }
  //设置饼图的option
  @action
  setPieChart(datas){
    let newdata = [{value:0, name:getLabel(382623,"任务数")},
    {value:0, name:getLabel(21984,"已超期")},
    {value:0, name:getLabel(1960,"进行中")},
    {value:0, name:getLabel(1961,"已完成")}];
    let _name = "";
    if(datas && datas.length>0){
        newdata = [];
        for(let i=0;i< datas.length;i++){
            newdata.push({value:datas[i].total, name:getLabel(382623,"任务数")});
            newdata.push({value:datas[i].overtime, name:getLabel(21984,"已超期")});
            newdata.push({value:datas[i].doing, name:getLabel(1960,"进行中")});
            newdata.push({value:datas[i].finish, name:getLabel(1961,"已完成")});
            _name = datas[i].showname;
        }
    }
    this.options = {
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        title : {
            text: getLabel(508808,'任务数量统计').replace('$workcode$',_name),
            //subtext: '纯属虚构',
            x:'center'
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data: [getLabel(382623,"任务数"),getLabel(21984,"已超期"),getLabel(1960,"进行中"),getLabel(1961,"已完成")]
        },
        series: [
            {
                name:_name,
                type:'pie',
                selectedMode: 'single',
                radius : '75%',
                center: ['50%', '50%'],
                label: {
                    normal: {
                        position: 'inner'
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data:newdata
            }            
        ]
      }
      this.isechart = 1;
  }
  //设置柱状图的option
  @action
  setBarChart(datas){
    let newdata = [];
    let titledata = [];
    let maxvalue = 0;
    if(datas && datas.length>0){
        for(let i=0;i< datas.length;i++){
            let curdata = [];
            titledata[i] = datas[i].showname;
            if(datas[i].total>maxvalue){
                maxvalue = datas[i].total;
            }
            curdata[0] = datas[i].total;
            curdata[1] = datas[i].overtime;
            curdata[2] = datas[i].doing;
            curdata[3] = datas[i].finish;
            newdata[i] = {
                name:datas[i].showname,
                type:'bar',
                // itemStyle:{
                //     normal:{
                //         color:'#2495f5'
                //     }
                // },
                data:curdata
            };
        }
    }
    this.options = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
            }
        },
        toolbox: {
            feature: {
                magicType: {show: true, type: ['line', 'bar']},
            },
            y:10,
            x:'90%'
        },
        legend: {
            data:titledata
        },
        xAxis: [
            {
                type: 'category',
                data: [getLabel(382623,"任务数"),getLabel(21984,"已超期"),getLabel(1960,"进行中"),getLabel(1961,"已完成")],
                axisPointer: {
                    type: 'shadow'
                }
            }
        ],
        // yAxis: [
        //     {
        //         type: 'value',
        //         name: '',
        //         min: 0,
        //         max: maxvalue+10,
        //         interval: 10,
        //         axisLabel: {
        //             formatter: '{value}'
        //         }
        //     }
        // ],
        yAxis: {},
        series: newdata
      }
      this.isechart = 1;
  }
  @action("任务导出")
  doExportData = (params) => {
        this.exportloading = true;
        params.type = this.selectedKey;
        TaskReport.doExportData(params).then(action((data) => {
            if (data.url) {
                window.location.href = `${location.origin}${data.url}`
            }
            this.exportloading = false;
        }))
  };
}